<template>
	<div class="login-wrap">
		<Verify
		    ref="verify"
		    :captcha-type="'clickWord'"
		    :img-size="{width:'400px',height:'200px'}"
		    @success="checkParam"
		/>
		<div class="ms-login">
			<div class="ms-title">约课管理系统</div>
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
				<el-form-item prop="username">
					<el-input v-model="ruleForm.username" placeholder="请输入账号">
						<el-button slot="prepend" icon="el-icon-lx-people"></el-button>
					</el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input type="password" placeholder="请输入密码" v-model="ruleForm.password"
						@keyup.enter.native="submitForm()">
						<el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
					</el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="submitForm()">登录</el-button>
				</div>
				<p class="login-tips">提示 : 请输入账号和密码。</p>
			</el-form>
		</div>
	</div>
</template>

<script>
	import Verify from "./../../components/verifition/Verify";
	export default {
		components: {Verify},
		data() {
			return {
				ruleForm: {
					username: '',
					password: ''
				},
				rules: {
					username: [{
						required: true,
						message: '请输入用户名',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			// 点击登录按钮
			submitForm() {
				let params = {
					username:this.ruleForm.username,
					password:this.ruleForm.password
				}
				if(params.username.length == 0){
				    return this.$message.error('账号不能为空')
				}
				if(params.password.length == 0){
				    return this.$message.error('密码不能为空')
				}
				 this.$refs.verify.show();
			},
			checkParam(){
			    let point_data  = JSON.parse(window.localStorage.getItem('point_data'));
			    let vm = this
			    this.$post(this.$api.homeLogin, {
			        captchaType:point_data.captchaType,
			        token:point_data.token,
			        pointJson:point_data.pointJson,
			        username:vm.ruleForm.username,
			        password:vm.ruleForm.password
			    }).then(function(res) {

			        if (res.code == 200) {
			            // 存储用户的token
			            localStorage.setItem('username',res.data.username);
			            localStorage.setItem('nickname',res.data.nickname);
			            localStorage.setItem('rolename',res.data.role_name);
			            localStorage.setItem('lastLoginTime',res.data.lastLoginTime);
			            localStorage.setItem('userid',res.data.id);
			            localStorage.setItem('roleid',res.data.role_id);
			            localStorage.setItem('token',res.data.Token);
			            vm.$message.success('登录成功！');
			            vm.$router.push({path:'/user'});
			        }else{
			            vm.$refs.verify.closeBox();
			            vm.$message.error(res.msg,10000)

			        }
			    });
			},

		}
	}
</script>

<style scoped>
	.login-wrap {
		position: relative;
		width: 100%;
		height: 100%;
		background-image: url(../../assets/img/login-bg.jpg);
		background-size: 100%;
	}

	.ms-title {
		width: 100%;
		line-height: 50px;
		text-align: center;
		font-size: 20px;
		color: #000;
		border-bottom: 1px solid #ddd;
	}

	.ms-login {
		position: absolute;
		left: 50%;
		top: 50%;
		width: 350px;
		margin: -190px 0 0 -175px;
		border-radius: 5px;
		background: rgba(255, 255, 255, 1);
		overflow: hidden;
	}

	.ms-content {
		padding: 30px 30px;
	}

	.login-btn {
		text-align: center;
	}

	.login-btn button {
		width: 100%;
		height: 36px;
		margin-bottom: 10px;
	}

	.login-tips {
		font-size: 12px;
		line-heigh: #000;
	}
</style>
